package cn.edu.scau.cmi.liweihang.hibernate.dao;

import java.util.Scanner;

import cn.edu.scau.cmi.liweihang.spring.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.liweihang.spring.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.liweihang.spring.hibernate.domain.Student;
import cn.edu.scau.cmi.liweihang.spring.hibernate.domain.Teacher;

public class Client {

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		StudentDAO sd = new StudentDAO();
		TeacherDAO td = new TeacherDAO();
		int choose = 1;
        print();
		while (choose != 0) {
			choose = scan.nextInt();
			scan.nextLine();
			switch (choose) {
			case 1: {				
                Student student=new Student();
                Long id=0l;
                String name=null;
                System.out.printf("请输入学生id:");
                id=scan.nextLong();
                scan.nextLine();
                System.out.printf("请输入学生姓名:");
                name=scan.nextLine();
                student.setId(id);
                student.setName(name);
                sd.save(student);
                System.out.println("学生添加成功，学生信息为："+student.toString());
                print();
                break;
			}
			case 2: {
				Student student=new Student();
                Long id=0l;
                System.out.printf("请输入要删除学生的id:");
                id=scan.nextLong();
                scan.nextLine();
                student.setId(id);
                if(sd.delete(student)!=null) {
                	System.out.println("学生删除成功");
                }else {
                   System.out.println("要删除的学生不存在");	
                }
                print();
                break;
			}
			case 3: {
				Student student=new Student();
				String name=null;
                Long id=0l;
                System.out.printf("请输入要修改名字学生的id:");
                id=scan.nextLong();
                scan.nextLine();
                System.out.printf("请输入要修改后的名字:");
                name=scan.nextLine();
                student.setId(id);
                sd.updateName(name, id);
                System.out.println("学生修改名字成功");
                print();
                break;
			}
			case 4: {
				Teacher teacher=new Teacher();
                Long id=0l;
                String name=null;
                System.out.printf("请输入教师id:");
                id=scan.nextLong();
                scan.nextLine();
                System.out.printf("请输入教师姓名:");
                name=scan.nextLine();
                teacher.setId(id);
                teacher.setName(name);
                td.save(teacher);
                System.out.println("教师添加成功，教师信息为："+teacher.toString());
                print();
                break;
			}
			case 5: {
				Teacher teacher=new Teacher();
                Long id=0l;
                System.out.printf("请输入要删除教师的id:");
                id=scan.nextLong();
                scan.nextLine();
                teacher.setId(id);
                if(td.findById(id)!=null) {
                	td.delete(teacher);
                    System.out.println("教师删除成功");
                }else {
                	System.out.println("要删除的教师不存在");
                }
               
                print();
                break;
			}
			case 6: {
				Teacher teacher=new Teacher();
				String name=null;
                Long id=0l;
                System.out.printf("请输入要修改名字教师的id:");
                id=scan.nextLong();
                scan.nextLine();
                System.out.printf("请输入要修改后的名字:");
                name=scan.nextLine();
                teacher.setId(id);
                td.updateName(name, id);
                System.out.println("教师名字成功");
                print();
                break;
			}
			case 7: {
				Student student=null;
                Long studentId=0l;
                Long teacherId=0l;
                System.out.printf("请输入要选择的指导教师的学生id:");
                studentId=scan.nextLong();
                scan.nextLine();
                System.out.printf("请输入要选择的指导教师的id:");
                teacherId=scan.nextLong();
                scan.nextLine();
                student=sd.updateTutor(studentId, teacherId);
                System.out.println("学生选择导师成功，新的学生信息为："+student);
                System.out.println("导师名字为："+td.findById(teacherId).getName());
                print();
                break;
			}
			case 8: {
				Student student=null;
                Long studentId=0l;
                Long teacherId=0l;
                System.out.printf("请输入要修改的指导教师的学生id:");
                studentId=scan.nextLong();
                scan.nextLine();
                System.out.printf("请输入要修改的指导教师的id:");
                teacherId=scan.nextLong();
                scan.nextLine();
                student=sd.updateTutor(studentId, teacherId);
                System.out.println("学生修改导师成功，新的学生信息为："+student);
                System.out.println("导师名字为："+td.findById(teacherId).getName());
                print();
                break;
			}
			case 9: {
                Student student=null;
				Long studentId=0l;
                Long teacherId=0l;
                System.out.printf("请输入要选择指导学生的教师id:");
                teacherId=scan.nextLong();
                scan.nextLine();
                System.out.printf("请输入要选择的指导学生的id:");
                studentId=scan.nextLong();
                scan.nextLine();
                student=td.updateTutor(teacherId, studentId);
                System.out.println("选择学生成功，学生信息更改为："+student);
                print();
                break;
			}
			case 10: {
                Student student=null;
				Long studentId=0l;
                Long teacherId=0l;
                System.out.printf("请输入要修改被指导学生的指导老师id:");
                teacherId=scan.nextLong();
                scan.nextLine();
                System.out.printf("请输入要被修改的指导学生的id:");
                studentId=scan.nextLong();
                scan.nextLine();
                student=td.updateTutor(teacherId, studentId);
                System.out.println("修改指导的学生成功，学生信息更改为："+student);
                print();
                break;
			}
			default: {
				System.out.println("谢谢使用");
			}
			}
		}
	}
	
	private static void print() {
		System.out.println("-------------------");
		System.out.println("请选择功能：");
		System.out.println("1、添加一个学生");
		System.out.println("2、删除一个学生");
		System.out.println("3、修改一个学生");
		System.out.println("4、添加一个老师");
		System.out.println("5、删除一个老师");
		System.out.println("6、修改一个老师");
		System.out.println("7、学生选择指导老师");
		System.out.println("8、学生修改指导老师");
		System.out.println("9、指导老师选择被指导的学生");
		System.out.println("10、指导老师修改被指导的学生");
		System.out.println("0、退出程序");
		System.out.println("-------------------");
	}

}
